// pages/member/addevaluate/addevaluate.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    value: 3,
    reComment: "",
    //评论内容
    comment: "",
    good: "",
    item: "",
    // 图片上传
    uploadUrl: "upload.uploadPic",
    fileList3: [],
    fileList4: [],
    upNumMaxOther: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        goods_id: Number(options.goods_id),
        order_id: Number(options.order_id),
        order_goods_id: Number(options.id)
      });
      this.getNetData();
    
  },

  getNetData () {
    let that = this;
    let urlStr = app.getNetAddresss("goods.comment.append-comment-page");
    app._postNetWork({
      url: urlStr,
      data: {
        goods_id: that.data.goods_id,
        order_id: that.data.order_id,
        order_goods_id: that.data.order_goods_id
      },
      success: (response) => {
        let res = response.data;
        if (res.result != 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        } else {
          this.setData({
            item: res.data.comment,
            value: res.data.comment.level,
            comment: res.data.comment.content,
          });
        }
      }
    });
  },

  commentChange(e) {
    let val = e.detail;
    this.setData({
      reComment: val,
    });
  },
  toReComment() {
    var that = this;
    if (this.data.reComment.length == 0) {
      wx.showModal({
        title: "提示",
        content: "您还没有输入相关的评论内容",
        success(res) {
          if (res.confirm) {
            that._submitData();
          }
        },
      });
    } else {
      this._submitData();
    }
  },
  //提交数据到服务器
  _submitData() {
    let urlStr = app.getNetAddresss("goods.comment.append-comment");
    let eltion = {
      id: this.data.item.id,
      order_goods_id: this.data.order_goods_id,
      content: this.data.reComment,
    };
    if (this.data.fileList3.length != 0) {
      let newArr2 = this.data.fileList3.map((item) => item.url);
      eltion.images = JSON.stringify(newArr2);
    }
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: eltion,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },

  //选择图片
  onRead_2() {
    if (this.data.upNumMaxOther == 5 || this.data.upNumMaxOther >= 5) {
      wx.showToast({
        icon: "none",
        title: "图片数量已达到上限",
        duration: 1500,
      });
      return false;
    }
    let that = this;
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    wx.chooseImage({
      count: 5 - this.data.upNumMaxOther,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        for (let tempFilePath of tempFilePaths) {
          wx.compressImage({
            src: tempFilePath, // 图片路径
            quality: 60, // 压缩质量
            success(e) {
              let photourl = e.tempFilePath;
              wx.uploadFile({
                url: urlStr,
                filePath: photourl,
                name: "file",
                formData: {
                  attach: "upload",
                },
                success(resdata) {
                  var res = JSON.parse(resdata.data);
                  if (res.result == 1) {
                    let fileList3 = that.data.fileList3;
                    let fileList4 = that.data.fileList4;
                    fileList3.push({
                      url: res.data.img,
                    });
                    fileList4.push({
                      url: photourl,
                    });
                    that.setData({
                      upNumMaxOther: that.data.upNumMaxOther + 1,
                      fileList3: fileList3,
                      fileList4: fileList4,
                    });
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: res.msg,
                      duration: 1500,
                    });
                  }
                },
              });
            },
            fail(e) {},
            complete(e) {},
          });
        }
      },
    });
  },
  removeImg_1: function (e) {
    let index = e.currentTarget.dataset.index;
    let fileList3 = this.data.fileList3;
    let fileList4 = this.data.fileList4;
    fileList3.splice(index, 1);
    fileList4.splice(index, 1);
    this.setData({
      fileList3: fileList3,
      fileList4: fileList4,
    });
    this.setData({
      upNumMaxOther: this.data.fileList3.length,
    });
  },
});
